package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.VIP_ZX_HorizontalListViewAdapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.VIP_JiLu;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.HorizontalListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DongTaiTG_DJ_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back, rl_geremtg_tgjl, rl_geremtg_ddjl;
    private String access_token;
    private Button btn_gerentg_count, btn_lv_item_open;
    private HorizontalListView main_hlv_vip;
    private ArrayList<VIP_JiLu> vip_jiLu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dongtaitg_dj);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        rl_geremtg_tgjl = findViewById(R.id.rl_geremtg_tgjl);
        rl_geremtg_ddjl = findViewById(R.id.rl_geremtg_ddjl);
        btn_gerentg_count = findViewById(R.id.btn_gerentg_count);
        main_hlv_vip = findViewById(R.id.main_hlv_vip);
        back.setOnClickListener(this);
        rl_geremtg_tgjl.setOnClickListener(this);
        rl_geremtg_ddjl.setOnClickListener(this);
        btn_gerentg_count.setOnClickListener(this);
        postData();
        main_hlv_vip.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                btn_lv_item_open = view.findViewById(R.id.btn_lv_item_open);
                btn_lv_item_open.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JSONObject object = new JSONObject();
                        try {
                            object.put("t_id", "");
                            object.put("s_id", vip_jiLu.get(position).getId());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        postUseTGData(object);
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_geremtg_ddjl:
                Intent intent = new Intent(this, GeRenTG_JiLu_Activity.class);
                intent.putExtra("access_token", access_token);
                intent.putExtra("type", 3);
                intent.putExtra("mtype", 2);
                startActivity(intent);
                break;
            case R.id.rl_geremtg_tgjl:
                Intent intent1 = new Intent(this, GeRenTG_JiLu_Activity.class);
                intent1.putExtra("access_token", access_token);
                intent1.putExtra("type", 4);
                intent1.putExtra("mtype", 2);
                startActivity(intent1);
                break;
            case R.id.btn_gerentg_count:
                Intent intent2 = new Intent(this, GeRenTG_Buy_Activity.class);
                intent2.putExtra("access_token", access_token);
                intent2.putExtra("mtype", 2);
                startActivity(intent2);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_my_dongtai_DJ_Url + access_token + "&type=2", null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        vip_jiLu = gson.fromJson(info.toString(), new TypeToken<ArrayList<VIP_JiLu>>(){}.getType());
                        // 获取屏幕宽高
                        int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽（像素，如：480px）
                        main_hlv_vip.setAdapter(new VIP_ZX_HorizontalListViewAdapter(DongTaiTG_DJ_Activity.this, screenWidth, null, null, 5, vip_jiLu, null));
                    } else {
                        ToolUtils.showToast(DongTaiTG_DJ_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(DongTaiTG_DJ_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postUseTGData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.gr_use_my_dongtai_Url + access_token, object, new Response.Listener<JSONObject>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("dongtai", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        Intent intent = new Intent(DongTaiTG_DJ_Activity.this, GeRenTG_Open_Activity.class);
                        intent.putExtra("access_token", access_token);
                        startActivity(intent);
                    } else {
                        ToolUtils.showToast(DongTaiTG_DJ_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(DongTaiTG_DJ_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        postData();
    }
}
